package com.anshi.shopchangepro.selfview;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.anshi.shopchangepro.R;
import com.anshi.shopchangepro.entry.Dynamic;
import com.anshi.shopchangepro.frag.CommentFrag;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DynamicAdapter extends BaseAdapter {
    private Context mContext;
    private List<Dynamic> dynamics=new ArrayList<>();
    private int itemLayoutRes;
    private ViewHolder holder=null;
    private CommentFrag commentFrag;
    public DynamicAdapter(Context mContext,List<Dynamic> dynamics, int itemLayoutRes,CommentFrag commentFrag){
        this.mContext = mContext;
        this.dynamics = dynamics;
        this.itemLayoutRes = itemLayoutRes;
        this.commentFrag=commentFrag;
    }
    @Override
    public int getCount() {
        if(null!=dynamics){
            return dynamics.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if(null!=dynamics){
            return dynamics.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view==null){
            view= LayoutInflater.from(mContext).inflate(R.layout.item_dynamic,null);
            holder=new ViewHolder();
            holder.ivDynamicUserImg=view.findViewById(R.id.iv_dynamic_userimg);
            holder.tvDynamicUserName=view.findViewById(R.id.tv_dynamic_username);
            holder.tvDynamicDynamicTime=view.findViewById(R.id.tv_dynamic_dynamictime);
            holder.tvDynmaicDynamicContent=view.findViewById(R.id.tv_dynamic_dynamiccontent);
            holder.gvDynamicDynamicImgs=view.findViewById(R.id.gv_dynamic_dynamicimgs);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        Dynamic dynamic=dynamics.get(i);
        Glide.with(mContext)
                .load(dynamic.getUserImg())
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true).circleCrop().into(holder.ivDynamicUserImg);
        holder.tvDynamicUserName.setText(dynamic.getUserName());
        holder.tvDynamicDynamicTime.setText(dynamic.getDynamicTime());
        holder.tvDynmaicDynamicContent.setText(decode(dynamic.getDynamicContent()));
        List<String> dynamicsimgs=dynamics.get(i).getDynamicImgs();
        DynamicImgAdapter imgAdapter=new DynamicImgAdapter(mContext,dynamics.get(i).getDynamicImgs(),R.layout.item_dynamic_img,commentFrag);
        if(dynamicsimgs.size()==0){
            holder.gvDynamicDynamicImgs.setVisibility(View.GONE);
        }

        holder.gvDynamicDynamicImgs.setAdapter(imgAdapter);
        return view;
    }
    class ViewHolder{
        ImageView ivDynamicUserImg;
        TextView tvDynamicUserName;
        TextView tvDynamicDynamicTime;
        TextView tvDynmaicDynamicContent;
        GridView gvDynamicDynamicImgs;
    }
    //将图片设置为圆形
    public Bitmap toRoundBitmap(Bitmap bitmap) {
        //圆形图片宽高
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //正方形的边长
        int r = 0;
        //取最短边做边长
        if (width > height) {
            r = height;
        } else {
            r = width;
        }
        //构建一个bitmap
        Bitmap backgroundBmp = Bitmap.createBitmap(width,
                height, Bitmap.Config.ARGB_8888);
        //new一个Canvas，在backgroundBmp上画图
        Canvas canvas = new Canvas(backgroundBmp);
        Paint paint = new Paint();
        //设置边缘光滑，去掉锯齿
        paint.setAntiAlias(true);
        //宽高相等，即正方形
        RectF rect = new RectF(0, 0, r, r);
        //通过制定的rect画一个圆角矩形，当圆角X轴方向的半径等于Y轴方向的半径时，
        //且都等于r/2时，画出来的圆角矩形就是圆形
        canvas.drawRoundRect(rect, r / 2, r / 2, paint);
        //设置当两个图形相交时的模式，SRC_IN为取SRC图形相交的部分，多余的将被去掉
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //canvas将bitmap画在backgroundBmp上
        canvas.drawBitmap(bitmap, null, rect, paint);
        //返回已经绘画好的backgroundBmp
        return backgroundBmp;
    }
    /**
     * 将取出内容解码
     * @param content
     * @return
     */
    public static String decode(String content) {
        final Pattern reUnicode = Pattern.compile("\\\\u([0-9a-zA-Z]{4})");

        if(content!=null){
            Matcher sMatcher = reUnicode.matcher(content);
            StringBuffer sb = new StringBuffer(content.length());
            while (sMatcher.find()) {
                sMatcher.appendReplacement(sb,
                        Character.toString((char) Integer.parseInt(sMatcher.group(1), 16)));
            }
            sMatcher.appendTail(sb);
            return sb.toString();
        }
        return "";
    }
}
